﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Studentperformancemanagement
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“schoolsDataSet.Students”中。您可以根据需要移动或删除它。
            this.studentsTableAdapter.Fill(this.schoolsDataSet.Students);

        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text=="")
            {
                MessageBox.Show("请输入学生姓名");
                string sql = "SELECT * FROM Students";
                dataGridView1.DataSource = Class1.Getdata(sql);
            }
            else
            {
                string sql = string.Format("SELECT * FROM Students where Name LIKE '%{0}%'",textBox1.Text);
                dataGridView1.DataSource = Class1.Getdata(sql);
            }
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            var open=form2.ShowDialog();
            string sql = "SELECT * FROM Students";
            dataGridView1.DataSource = Class1.Getdata(sql);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int id = (int)dataGridView1.SelectedRows[0].Cells[0].Value;
            string name = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
            int age = (int)dataGridView1.SelectedRows[0].Cells[2].Value;
            int score = (int)dataGridView1.SelectedRows[0].Cells[3].Value;
            Form2 form2 = new Form2(id,name,age,score);
            var open = form2.ShowDialog();
            string sql = "SELECT * FROM Students";
            dataGridView1.DataSource = Class1.Getdata(sql);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            var hint = MessageBox.Show($"是否删除该数据！","", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (hint.Equals(DialogResult.OK))
            {
                int Id = (int)dataGridView1.SelectedRows[0].Cells[0].Value;
                string sql1 = string.Format("delete from Students where Id={0}", Id);
                Class1.Adddeletemodify(sql1);
                MessageBox.Show("删除成功！");
            }
            string sql = "SELECT * FROM Students";
            dataGridView1.DataSource = Class1.Getdata(sql);

        } 
    }
}
